package com.xqboss.apps.service.equipment;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xqboss.apps.domain.equipment.EquipmentUpgradeConfig;
import com.xqboss.apps.domain.equipment.EquipmentUpgradeConfigCoefficient;
import com.xqboss.apps.enums.equipment.UpgradeConfigCoefficientEnum;
import com.xqboss.apps.mapper.equipment.EquipmentUpgradeConfigCoefficientMapper;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * 装备升级加点-配置(瑕疵,耐久,破损)接口
 *
 * @author wangcong
 * @date 2023-11-23
 */
@Service
public class EquipmentUpgradeConfigCoefficientService extends ServiceImpl<EquipmentUpgradeConfigCoefficientMapper, EquipmentUpgradeConfigCoefficient> {

    /**
     * 根据概率获取爆率信息
     * @param type
     * @param upgradeConfig
     * @return
     */
    public EquipmentUpgradeConfigCoefficient randomCoefficient(UpgradeConfigCoefficientEnum type, EquipmentUpgradeConfig upgradeConfig){
        List<EquipmentUpgradeConfigCoefficient> list = list(new LambdaQueryWrapper<EquipmentUpgradeConfigCoefficient>()
                .eq(EquipmentUpgradeConfigCoefficient::getType, type)
                .eq(EquipmentUpgradeConfigCoefficient::getUpgradeConfigId, upgradeConfig.getId()));
        BigDecimal random = RandomUtil.randomBigDecimal(BigDecimal.ZERO, BigDecimal.ONE);
        for (EquipmentUpgradeConfigCoefficient coefficient : list) {
            if(random.compareTo(coefficient.getBurstRate()) < 0){
                return coefficient;
            }
            random = random.subtract(coefficient.getBurstRate());
        }
        return list.get(list.size() - 1);
    }

    /**
     * 根据升级配置id获得配置的系数
     */
    public List<EquipmentUpgradeConfigCoefficient> getCoefficientByUpgradeConfigId(Long upgradeConfigId, UpgradeConfigCoefficientEnum type){
        return list(new LambdaQueryWrapper<EquipmentUpgradeConfigCoefficient>()
                .eq(EquipmentUpgradeConfigCoefficient::getUpgradeConfigId, upgradeConfigId)
                .eq(EquipmentUpgradeConfigCoefficient::getType, type));
    }
}
